#include <windows.h>
#include <commctrl.h>
#include <cstdlib>
#include <cstring>

int LvInsColumn(HWND hLv,const char* szText,int iIndex,int iWidth) {
	LVCOLUMN vcl;
	vcl.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
	vcl.pszText = (char*)szText;
	vcl.cx = iWidth;
	vcl.iSubItem = 0;

	return ListView_InsertColumn(hLv, iIndex, &vcl);
}

int LvAddItem(HWND hLv,const char* szText,int iIndex,int iSubItem) {
	LVITEMA lvi;
	lvi.mask=LVIF_TEXT;
	lvi.iItem=iIndex;
	lvi.iSubItem=iSubItem;
	lvi.pszText=(char*)szText;

	return ListView_InsertItem(hLv,&lvi);
}

int LvAddItemF(HWND hLv,int iIndex,int iSub,const char* szFormat,...) {
	char bfrOut[400]= {0};
	va_list vars;
	va_start(vars,szFormat);
	vsprintf(bfrOut,szFormat,vars);
	va_end(vars);
	return LvAddItem(hLv,bfrOut,iIndex,iSub);
}

int LvSetItem(HWND hLv,const char* szText,int iIndex,int iSubItem) {
	LVITEMA lvi;
	lvi.mask=LVIF_TEXT;
	lvi.iItem=iIndex;
	lvi.iSubItem=iSubItem;
	lvi.pszText=(char*)szText;

	return ListView_SetItem(hLv,&lvi);
}

int LvSetItemF(HWND hLv,int iIndex,int iSub,const char* szFormat,...) {
	char bfrOut[400]= {0};
	va_list vars;
	va_start(vars,szFormat);
	vsprintf(bfrOut,szFormat,vars);
	va_end(vars);
	return LvSetItem(hLv,bfrOut,iIndex,iSub);
}

int initLv(HWND hLv) {
	ListView_SetExtendedListViewStyle
	(hLv,LVS_EX_ONECLICKACTIVATE
	 |LVS_EX_FULLROWSELECT);
	ListView_SetItemCount(hLv, 10000);
	return 0;
}

int ListView_SelAndScroll(HWND hlList,int iIndex) {
	RECT rc;
	ListView_GetItemRect(hlList,iIndex,&rc,LVIR_BOUNDS);
	ListView_Scroll(hlList,0,rc.top-(rc.bottom-rc.top)*5);
	ListView_SetItemState(hlList,iIndex,LVIS_SELECTED,LVIS_SELECTED);
	return 0;
}
